package com.zhupanlin.config;

import org.apache.catalina.filters.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.util.Arrays;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: 自定义 mvc 配置类
 * @date 2024/12/21 16:27
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /*@Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        FilterRegistrationBean<CorsFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(false);
        corsConfiguration.setAllowedHeaders(Arrays.asList("*"));
        corsConfiguration.setAllowedMethods(Arrays.asList("*"));
        corsConfiguration.setAllowedOrigins(Arrays.asList("*"));
        corsConfiguration.setMaxAge(3600L);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        filterFilterRegistrationBean.setFilter(new CorsFilter());
        filterFilterRegistrationBean.setOrder(-1);
        return filterFilterRegistrationBean;

    }*/

    /**
     * 用来处理全局跨域
     * @param registry
     */
    /*@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对哪些请求进行跨域处理
                .allowedMethods("*")
                .allowCredentials(false)
                .allowedHeaders("*")
                .allowedOrigins("*")
                .maxAge(3600);
        WebMvcConfigurer.super.addCorsMappings(registry);
    }*/
}
